<script lang="ts" setup>
import { ref, onActivated } from 'vue';
import { $fetch } from '@/settings/utils/fetch';
import useNotification from '@/settings/hooks/notification';
import { dialog } from '@/settings/components/dialog';
import markdownit from 'markdown-it';
import { format, parseISO } from 'date-fns';
import { useI18n } from 'vue-i18n';

const listing = ref([]);
const pager = ref({});
const loading = ref(false);
const search = ref();
const { success } = useNotification();
const { t } = useI18n();

const form = ref({
  name: '',
  createrName: '',
});

const refresh = async (page = 1) => {
  const { isSuccess, data } = await $fetch('/admin/email/temp/getEmailTempList', {
    method: 'post',
    data: {
      emailTemplateName: form.value.name,
      createName: form.value.createrName,
      pageNum: page,
      pageSize: 20,
    },
    loadingStatus: loading,
  });

  if (isSuccess) {
    listing.value = data.records;

    pager.value = {
      current: page,
      total: data.total,
    };
  }
}

const remove = async (id: string) => {
  const { isSuccess } = await $fetch('/admin/email/temp/deleteEmailTemp', {
    method: 'post',
    data: {
      id,
    },
    loadingStatus: loading,
  });

  if (isSuccess) {
    success(t('settings.notification.del'));
    search.value.refresh();
  }
};

onActivated(() => {
  search.value.refresh('reset');
});

const showPreview = async (id: string) => {
  const { isSuccess, data } = await $fetch('/admin/email/temp/getEmailById', {
    data: {
      emailId: id,
    },
  });

  if (isSuccess) {
    const md = markdownit();

    await dialog(() => import('../user/preview-window.vue'), {
      props: {
        title: data.emailTemplateTitle,
        content: md.render(data.emailTemplateContent ?? ''),
      },
    });
  }
}
</script>

<template>
  <OpenPageSection>
    <OpenSearchTable :pager="pager" :data="listing" :loading="loading" @search="refresh" ref="search">
      <template #header>
        <AFormItem :label="$t('settings.view.subscription.template.index.column1')">
          <AInput v-model="form.name" allow-clear />
        </AFormItem>
        <AFormItem :label="$t('settings.view.subscription.template.index.column2')">
          <AInput v-model="form.createrName" allow-clear />
        </AFormItem>
      </template>

      <template #header-button>
        <OpenRouterButton to="/operator/subscription/template/add" type="primary">{{ $t('settings.action.add') }}</OpenRouterButton>
      </template>

      <template #columns>
        <ATableColumn :title="$t('settings.view.subscription.template.index.column1')" data-index="emailTemplateName" />
        <ATableColumn :title="$t('settings.view.subscription.template.index.column3')" data-index="emailTemplateTitle" />
        <ATableColumn :title="$t('settings.view.subscription.template.index.column2')" data-index="createName" />

        <ATableColumn :title="$t('settings.view.subscription.template.index.column4')" :width="260">
          <template #cell="{ record }">
            {{ format(parseISO(record.createTime), 'yyyy-MM-dd HH:mm:ss') }}
          </template>
        </ATableColumn>

        <ATableColumn :title="$t('settings.view.subscription.template.index.column5')" :width="260">
          <template #cell="{ record }">
            <div class="button-actions">
              <AButton size="small" @click="showPreview(record.id)">{{ $t('settings.action.preview') }}</AButton>
              <OpenRouterButton size="small" :to="`/operator/subscription/template/edit/${record.id}`">{{ $t('settings.action.edit') }}</OpenRouterButton>
              <AButton type="primary" status="danger" size="small" @click="remove(record.id)" v-confirm>{{ $t('settings.action.delete') }}</AButton>
            </div>
          </template>
        </ATableColumn>
      </template>
    </OpenSearchTable>
  </OpenPageSection>
</template>

<style lang="scss" scoped>
</style>
